home *** CD-ROM | disk | FTP | other *** search
- /* xigc.c
- * S.Coffin USWAT 1/93
- *
- * Main X11 program for displaying Goban widget, menus, and etc
- */
-
- /* Copyright (c) 1992 by Stephen Coffin. All rights reserved.
- *
- * This program is distributed in the hope that it will be useful.
- * Use and copying of this software and preparation of derivative works
- * based upon this software are permitted, so long as the following
- * conditions are met:
- * o credit to the authors is acknowledged following current
- * academic behavior
- * o no fees or compensation are charged for use, copies, or
- * access to this software
- * o this copyright notice is included intact.
- * This software is made available AS IS, and no warranty is made about
- * the software or its performance.
- *
- * Bug descriptions, use reports, comments or suggestions are welcome.
- * Send them to scoffin@advtech.uswest.com
- */
-
- /* uncomment this if you get X error "Attempt to access private resource
- * failed" (XGrabKey) =SC
- */
- /*SC #define NOKEYGRAB SC*/
-
- #include <stdio.h>
- #include <X11/Intrinsic.h>
- #include <X11/Shell.h>
- #include <X11/StringDefs.h>
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- #include <X11/cursorfont.h>
- #include <X11/Xaw/Command.h>
- #include <X11/Xaw/Form.h>
- #include <X11/Xaw/Box.h>
- #include "filter.h"
- #include "shared.h"
- #include "stipple.bm"
-
- /* the default communications command to execute */
- #define CMD "igscomm -h 128.91.11.53 -p 6969"
- /* the command to kill the communications proc */
- #define QUITCMD "."
-
- /* Use of telnet not recommended =SC */
- /* #define CMD "telnet 128.91.11.53 6969" */
- /* #define QUITCMD "\nquit" */
-
- /* #define CMD "rlogin uswat" */
- /* #define QUITCMD "~." */
-
- /* #define CMD "cu uswat" */
- /* #define QUITCMD "~." */
-
- #define USAGE "%s [x-arg-list] [-?] [-c \"command line to execute\"] [-d] [-s]\n"
- #define TITLE "xigc Menu"
- #define ICONTITLE "xigc Menu"
- #define BOARDTITLE "xigc"
- #define BOARDICON "xigc Board"
-
- unsigned char cmdline[80];
- extern int move;
- extern int stepmove;
- int currentmove;
- unsigned char stippleflag = FALSE;
- unsigned char *cmd[80], *quitcmd[80];
- static void ClearBoard();
- int tersemode = FALSE;
- int sortwhomode = FALSE;
- extern unsigned char my_moveflag;
- extern int putout();
-
- #include "Goban.h"
- void SetCursor ();
- void read_filter(), read_kbd(), read_pipe();
- int setup_filter();
- int input0id, input1id, input2id;
- int i0id, i1id, i2id;
- XtAppContext app_context;
- int dbg = FALSE;
- static int cmdflag = FALSE;
- static int saveformat = TEXT;
- static int get_cmd();
- int boardchanged = FALSE;
- int mouseflag = FALSE;
- unsigned char statusflag = FALSE;
- static unsigned char savedir[80];
-
- /* these fp's are the std i/o when we EXEC a process */
- FILE *exec_stdin=NULL, *exec_stdout=NULL, *exec_stderr=NULL;
- int gameno=(-1);
- int handicap = 0;
- extern unsigned char observeflag;
- int boardsize = 19;
- unsigned char player1[20], player2[20];
- int p1_sec, p2_sec;
- int p1_captured, p2_captured;
- int p1_byoyomi, p2_byoyomi;
- extern char rank1[], rank2[];
- extern float komi;
-
- Widget toplevel, bw, goban, refresh, clearb, form, size, coordinates;
- Widget stepf, stepb, refreshstep, savecurrent;
- Widget cursorcolor, savegame, localrefresh, quit;
- Widget gamew, hcapw, whitew, blackw, captw, captb, timew, timeb, lastw;
-
- #define MAXSIZE 19
- int colorflag = BLACK;
-
- beep() {
- XBell( XtDisplay(toplevel), 0 );
- }
-
- highlight( flag )
- int flag;
- {
- static int ff = FALSE;
- int j;
- Arg args[2];
- Pixel fg, bg, xx;
-
- /* switch highlighting */
- if( (flag && !ff) || (!flag && ff) ) {
- j = 0;
- XtSetArg( args[j], XtNforeground, &fg ); ++j;
- XtSetArg( args[j], XtNbackground, &bg ); ++j;
- XtGetValues( localrefresh, args, j );
- xx = fg;
- fg = bg;
- bg = xx;
- j = 0;
- XtSetArg( args[j], XtNforeground, fg ); ++j;
- XtSetArg( args[j], XtNbackground, bg ); ++j;
- XtSetValues( localrefresh, args, j );
-
- ff = flag;
- }
- }
-
- clearit() {
- GbClearBoard( goban );
- }
-
- void Quit( w, client_data, call_data )
- GobanWidget w;
- XtPointer client_data, call_data;
- {
- if( cmdflag ) {
- beep();
- fprintf( stderr,
- "\nLOCAL: you must log out of IGS and each intermedate system manually\n" );
- fprintf( stderr, " when using the \"-c\" option\n" );
- fprintf( exec_stdin, "\n" );
- fflush( exec_stdin );
- return;
- }
-
- fprintf( exec_stdin, "quit\n" );
- fflush( exec_stdin );
- sleep( 1 );
-
- if( quitcmd[0] == '\0' )
- fprintf( exec_stdin, "\n%s\n", QUITCMD );
- else
- fprintf( exec_stdin, "\n%s\n", quitcmd );
- fflush( exec_stdin );
- sleep( 1 );
- rdwr_pclose( NULL, NULL, NULL );
- exit( 0 );
- }
-
- static void SaveGame( w, client_data, call_data )
- GobanWidget w;
- XtPointer client_data, call_data;
- {
- unsigned char s[40], s2[40];
- int ret;
- struct stat sbuf;
-
- if( dbg )
- fprintf( stderr, "IN SAVEGAME call_data=0x%x\n", client_data );
-
- if( (client_data == (XtPointer)REALMOVE && (gameno < 0 || move < 1) ) ||
- (client_data == (XtPointer)STEPMOVE && currentmove < 0 ) ) {
- beep();
- fprintf( stderr, "\nLOCAL: No active game to save\n" );
- fflush( stderr );
- }
- else {
- if( (int)strlen( (char *)player1 ) <= 0 || (int)strlen( (char *)player2 ) <= 0 )
- strcpy( (char *)s, "no_players" );
- else sprintf( (char *)s, "%s-%s", player1, player2 );
-
- /* find an unused filename */
- sprintf( (char *)s2, "%s/%s", savedir, s );
- for( ret = 1 ;; ++ret ) {
- if( stat( (char *)s2, &sbuf ) == 0 ) {
- sprintf( (char *)s2, "%s/%s.%d", savedir, s, ret );
- continue;
- }
- else break;
- }
-
- if( saveformat == SGF )
- ret = save_game_sgf( s2, player1, player2, handicap,
- client_data );
- else
- ret = save_game( s2, player1, player2, handicap, client_data );
- if( ret ) {
- fprintf( stderr, "\nLOCAL: Game saved to %s\n", s2 );
- fflush( stderr );
- }
- }
- fprintf( exec_stdin, "\n" );
- fflush( exec_stdin );
- return;
- }
-
- set_cursor_color( m )
- int m;
- {
- if( m % 2 ) {
- colorflag = BLACK;
- SetCursor( GbCBlackStone );
- }
- else {
- colorflag = WHITE;
- SetCursor( GbCWhiteStone );
- }
- return( colorflag );
- }
-
-
- void do_refresh( m, type )
- int m;
- int type;
- {
- int i, x, y, flag;
-
- GbClearBoard( goban );
- initboard( boardsize );
-
- /* handle handicap */
- if( handicap > 0 ) {
- sethandicap( handicap, type );
- i = 1;
- }
- else i = 0;
-
- update_handicap( handicap, komi );
-
- for( ; i <= m; ++i ) {
- getmove( i, &x, &y, &flag );
- if( i%2 ) {
- makemove( x, y, i, GbCWhiteStone, type );
- }
- else {
- makemove( x, y, i, GbCBlackStone, type );
- }
- }
-
- set_cursor_color( m );
- }
-
- void RefreshStep( w, client_data, call_data )
- GobanWidget w;
- XtPointer client_data, call_data;
- {
- int i;
- int x, y;
-
- if( move < 0 || gameno < 0 ) {
- beep();
- highlight( FALSE );
- fprintf( stderr, "\nLOCAL: No active game to refresh\n" );
- fflush( stderr );
- fprintf( exec_stdin, "\n" );
- fflush( exec_stdin );
- return;
- }
- else if( stepmove < 0 || statusflag ) {
- beep();
- return;
- }
-
- boardchanged = TRUE;
- mouseflag = FALSE;
- do_refresh( stepmove, STEPMOVE );
- currentmove = stepmove;
- if( stepmove >= move ) {
- boardchanged = FALSE;
- highlight( FALSE );
- }
- }
-
- void LocalRefresh( w, client_data, call_data )
- GobanWidget w;
- XtPointer client_data, call_data;
- {
- int i;
- int x, y;
-
- boardchanged = mouseflag = statusflag = FALSE;
-
- if( move < 0 || gameno < 0 ) {
- beep();
- highlight( FALSE );
- fprintf( stderr, "\nLOCAL: No active game to refresh\n" );
- fflush( stderr );
- highlight( FALSE );
- GbClearBoard( goban );
- fprintf( exec_stdin, "\n" );
- fflush( exec_stdin );
- initboard( boardsize );
- return;
- }
-
- do_refresh( move, REALMOVE );
- stepmove = currentmove = move;
- highlight( FALSE );
- update_status( gameno, player2, player1, p2_captured, p1_captured,
- p2_sec, p1_sec, p2_byoyomi, p1_byoyomi, rank1, rank2 );
- update_handicap( handicap, komi );
- }
-
- static unsigned char keyecho = TRUE;
-
- /* This one processes keystrokes when the cursor is in the goban or menu
- *
- * adapted from code donated by loganj@byu.edu
- *
- * CNTRL-P causes echo to be suppressed until the next newline
- */
-
- #ifndef NOKEYGRAB
- static void keyevent( w, client_data, event, continue_flag ) /* BYU */
- Widget w;
- XtPointer client_data;
- XEvent *event;
- Boolean *continue_flag;
- {
- int i;
- KeySym ks;
- XComposeStatus compose;
- KeySym mykey;
- static int tx = 0;
- static unsigned char bst[4] = { 8,32,8,0 };
- static unsigned char eol[3] = { 13,10,0 };
- static unsigned char keytext[32], sendtext[128];
-
- switch (event->type) {
- case KeyPress :
- i = XLookupString( &event->xkey, (char *)keytext, 32, &mykey,
- &compose );
-
- if( dbg ) fprintf( stderr,
- "In keyevent KEYPRESS [i=%d, mykey=0x%x tx=%d] keytext=>>%s<<\n",
- i, mykey, tx, keytext );
-
- if( i >= 1 && mykey >= 0 ) {
- if( keytext[0] == '\n' || keytext[0] == '\r') { /* newline */
- fputs( (char *)eol, stderr );
-
- sendtext[tx++] = '\n';
- sendtext[tx] = 0;
-
- /* got a line, now process it and send */
- process_kbd( sendtext, exec_stdin );
- tx = 0;
- keyecho = TRUE; /* Allow 1 line of no echo */
- }
- else if( keytext[0] == 0x15 ) { /* delete */
- while( tx > 0 ) {
- tx--;
- if( keyecho ) fputs( (char *)bst, stderr );
- }
- }
- /* backspace */
- else if( keytext[0] == 0x08 || mykey == (KeySym)(-1) ) {
- if( tx > 0 ) {
- tx--;
- if( keyecho ) fputs( (char *)bst, stderr );
- }
- }
- else if( keytext[0] == 0x10 ) { /* CNTRL-P */
- if( keyecho ) keyecho = FALSE;
- else keyecho = TRUE;
- }
- else if( tx < 127 && keytext[0] >= 0x20 ) { /* normal char */
- sendtext[tx++] = keytext[0];
- keytext[1] = 0;
- if( keyecho ) fputs( (char *)keytext, stderr );
- }
- }
- break;
- case MappingNotify:
- XRefreshKeyboardMapping (&(event->xmapping));
- break;
- default:
- if( dbg ) fprintf( stderr, "In keyevent Unknown Type 0x%x\n",
- event->type );
- break;
- }
- fflush( stderr );
- }
- #endif /* NOKEYGRAB */
-
- static void CursorColor( w, client_data, call_data )
- GobanWidget w;
- XtPointer client_data, call_data;
- {
- makemove( (-1), (-1), ++currentmove, EMPTY, STEPMOVE );
- boardchanged = mouseflag = TRUE;
- highlight( TRUE );
- set_cursor_color( colorflag+1 );
- }
-
- static void StepForward( w, client_data, call_data )
- GobanWidget w;
- XtPointer client_data, call_data;
- {
- int i, x, y, flag;
-
- if( move < 0 ) {
- beep();
- highlight( FALSE );
- fprintf( stderr, "\nLOCAL: No active game to step\n" );
- fflush( stderr );
- fprintf( exec_stdin, "\n" );
- fflush( exec_stdin );
- return;
- }
-
- if( stepmove >= move || statusflag ) {
- beep();
- return;
- }
- else if( stepmove < 0 ) stepmove = 0;
- else {
- ++stepmove;
- currentmove = stepmove;
- }
-
- if( dbg )
- fprintf( stderr, "StepForward entered [stepmove=%d, move=%d]\n",
- stepmove, move );
-
- boardchanged = TRUE;
-
- if( mouseflag ) {
- do_refresh( stepmove, STEPMOVE );
- mouseflag = FALSE;
- }
- else {
- if( stepmove == 0 && handicap > 0 ) {
- sethandicap( handicap, STEPMOVE );
- }
- else {
- getmove( stepmove, &x, &y, &flag );
- if( stepmove % 2 ) {
- GbSetPoint( goban, x, y, GbWhiteStone, TRUE );
- makemove( x, y, stepmove, GbWhiteStone, STEPMOVE );
- }
- else {
- GbSetPoint( goban, x, y, GbBlackStone, TRUE );
- makemove( x, y, stepmove, GbBlackStone, STEPMOVE );
- }
- }
- }
-
- if( stepmove < move ) boardchanged = TRUE;
- else boardchanged = FALSE;
-
- set_cursor_color( stepmove );
- if( stepmove >= move ) highlight( FALSE );
- }
-
- static void StepBack( w, client_data, call_data )
- GobanWidget w;
- XtPointer client_data, call_data;
- {
- int x, y, flag;
-
- if( dbg ) fprintf( stderr, "StepBack entered [stepmove=%d, move=%d]\n",
- stepmove, move );
-
- if( move < 0 ) {
- beep();
- highlight( FALSE );
- fprintf( stderr, "\nLOCAL: No active game to step\n" );
- fflush( stderr );
- fprintf( exec_stdin, "\n" );
- fflush( exec_stdin );
- return;
- }
-
- if( stepmove < 0 || statusflag ) {
- beep();
- return;
- }
- else if( stepmove > move ) stepmove = move;
- else if( stepmove == 0 ) ClearBoard();
- else {
-
- boardchanged = TRUE;
-
- /* only do full refresh if this move caused any captures, or if
- * the board is dirty
- */
- getmove( stepmove, &x, &y, &flag );
- if( !flag && !mouseflag ) {
- GbSetPoint( goban, x, y, GbEmptyPoint, TRUE );
- removemove( stepmove, STEPMOVE );
- getmove( stepmove-1, &x, &y, &flag );
- GbSetPoint( goban, x, y, (((stepmove-1)%2)+1), TRUE );
- }
- else {
- do_refresh( stepmove-1, STEPMOVE );
- mouseflag = FALSE;
- }
- }
-
- --stepmove;
- currentmove = stepmove;
- set_cursor_color( stepmove );
- highlight( TRUE );
- }
-
- static void InputStone( w, event )
- GobanWidget w;
- XButtonEvent *event;
- {
- Position x = event->x;
- Position y = event->y;
-
- if( GbGetStoneFromPosition( w, &x, &y ) == TRUE ) {
- if( dbg ) fprintf( stderr, "Input stone at %c%d\n",
- 'A' + (x > 8 ? x + 1 : x) - 1, y );
- }
- else {
- beep();
- return;
- }
-
- if( observeflag != PLAYING && observeflag != SCORING ) {
- beep();
- return;
- }
-
- fprintf( exec_stdin, "%c%d\n", 'A' + (x > 8 ? x + 1 : x) - 1, y );
- fflush( exec_stdin );
- if( !tersemode ) {
- putout( "\n", FALSE, FALSE, 46 );
- }
- my_moveflag = TRUE;
- }
-
- static void AlternateStone( w, event )
- GobanWidget w;
- XButtonEvent *event;
- {
- int ob;
- Position x = event->x;
- Position y = event->y;
-
- if( GbGetStoneFromPosition( w, &x, &y ) == TRUE ) {
- if( dbg ) fprintf( stderr, "Alternate stone at %c%d\n",
- 'A' + (x > 8 ? x + 1 : x) - 1, y );
- }
- else {
- beep();
- return;
- }
-
- if( statusflag ) {
- beep();
- return;
- }
-
- ++currentmove;
- if( gameno < 0 ) gameno = 0;
- boardchanged = mouseflag = TRUE;
- if( colorflag == BLACK ) {
- GbSetPoint( goban, x, y, GbBlackStone, TRUE );
- makemove( x, y, currentmove, GbBlackStone, STEPMOVE );
- }
- else {
- GbSetPoint( goban, x, y, GbWhiteStone, TRUE );
- makemove( x, y, currentmove, GbWhiteStone, STEPMOVE );
- }
- set_cursor_color( colorflag+1 );
- highlight( TRUE );
- }
-
-
- static void RemoveStone( w, event )
- GobanWidget w;
- XButtonEvent *event;
- {
- Position x = event->x;
- Position y = event->y;
-
- if (GbGetStoneFromPosition (w, &x, &y) == TRUE) {
- if( dbg ) fprintf ( stderr, "Remove stone at %c%d\n",
- 'A' + (x > 8 ? x + 1 : x) - 1, y);
- }
-
- if( statusflag ) {
- beep();
- return;
- }
-
- /* XXX there is a slight glitch here: we do not remove stones
- * from the board table, so they still interact when testing
- * for dead stone removal and "Refresh(Local)"
- */
- GbSetPoint( goban, x, y, GbEmptyPoint, TRUE );
-
- boardchanged = mouseflag = TRUE;
- highlight( TRUE );
- }
-
- void Ticker( client_data, timer )
- XtPointer client_data;
- XtIntervalId *timer;
- {
- XtAppAddTimeOut( (XtAppContext)client_data, 1000, Ticker, client_data );
- if( move < 0 || !observeflag || statusflag ) return;
-
- else if( move % 2 ) {
- if( FALSE ) fprintf( stderr, "TIMER EVEN [B]\n" );
- --p2_sec;
- fix_btime( p2_sec, p2_byoyomi );
- }
- else {
- if( FALSE ) fprintf( stderr, "TIMER ODD [W]\n" );
- --p1_sec;
- fix_wtime( p1_sec, p1_byoyomi );
- }
- }
-
- void SetCursor ( cs )
- int cs;
- {
- Arg args[1];
-
- switch (cs) {
- case GbCBlackStone :
- XtSetArg (args[0], XtNcursor, GbCBlackStone);
- if( dbg ) fprintf ( stderr, "Set cursor to : Black \n");
- break;
- case GbCWhiteStone :
- XtSetArg (args[0], XtNcursor, GbCWhiteStone );
- if( dbg ) fprintf ( stderr, "Set cursor to : White\n");
- break;
- case XC_question_arrow :
- XtSetArg (args[0], XtNcursor, XC_question_arrow );
- if( dbg ) fprintf ( stderr, "Set cursor to : Question\n");
- break;
- }
-
- XtSetValues (goban, args, 1);
- }
-
- void RefreshHost( w, client_data, call_data )
- GobanWidget w;
- XtPointer client_data, call_data;
- {
- if( gameno < 0 ) {
- beep();
- highlight( FALSE );
- fprintf( stderr, "\nLOCAL: No active game to refresh\n" );
- fflush( stderr );
- highlight( FALSE );
- GbClearBoard( goban );
- initboard( boardsize );
- fprintf( exec_stdin, "\n" );
- fflush( exec_stdin );
- }
- else {
- SetCursor ( GbCBlackStone );
- move = (-1);
- refreshboard();
- fprintf( exec_stdin, "moves %d\n", gameno );
- fflush( exec_stdin );
- }
- }
-
- int refreshboard() {
- if( gameno < 0 ) {
- highlight( FALSE );
- fprintf( stderr, "\nLOCAL: No active game to refresh\n" );
- fflush( stderr );
- fprintf( exec_stdin, "\n" );
- fflush( exec_stdin );
- return;
- }
-
- boardchanged = mouseflag = statusflag = FALSE;
- highlight( FALSE );
- GbClearBoard( goban );
- stepmove = (-1);
- currentmove = (-1);
- handicap = 0;
- initboard( boardsize );
- }
-
- static void ClearBoard() {
- GbClearBoard( goban );
- stepmove = (-2);
- currentmove = (-1);
- boardchanged = TRUE;
- mouseflag = FALSE;
- highlight( TRUE );
- set_cursor_color( 1 );
- clear_stepboard();
- }
-
- void SetSize( w, client_data, call_data )
- GobanWidget w;
- XtPointer client_data, call_data;
- {
-
- Arg args[1];
- static int beenhere = FALSE;
-
- if( !beenhere ) boardsize = 13;
-
- if( call_data == NULL ) {
- switch( boardsize ) {
- case 9:
- boardsize = 13;
- break;
- case 13:
- boardsize = 19;
- break;
- case 19:
- default:
- boardsize = 9;
- break;
- }
- }
-
- if( dbg ) fprintf ( stderr, "Set size to : %d\n", boardsize );
-
- if( beenhere ) {
- stepmove = (-2);
- currentmove = (-1);
- boardchanged = FALSE;
- mouseflag = FALSE;
- highlight( FALSE );
- initboard( boardsize );
- }
- else beenhere = TRUE;
-
- set_cursor_color( 1 );
-
- XtSetArg( args[0], XtNgameSize, (Dimension)boardsize );
- XtSetValues( goban, args, 1 );
-
- }
-
- static void SetCoordinates() {
- Arg args[1];
- Boolean coordinates;
-
- XtSetArg (args[0], XtNdisplayCoordinates, &coordinates);
- XtGetValues (goban, args, 1);
-
- switch (coordinates) {
- case FALSE :
- XtSetArg (args[0], XtNdisplayCoordinates, TRUE) ;
- if( dbg ) fprintf( stderr, "Set coordinates to : TRUE \n");
- break;
- case TRUE :
- XtSetArg (args[0], XtNdisplayCoordinates, FALSE);
- if( dbg ) fprintf( stderr, "Set coordinates to : FALSE \n");
- break;
- }
-
- XtSetValues (goban, args, 1);
- }
-
- static void SetupBoard() {
- unsigned char s[40];
-
- XtPopup( bw, XtGrabNone );
- SetSize( NULL, NULL, NULL );
- set_cursor_color( 1 );
- sprintf( (char *)s, "%s [%s]", BOARDTITLE, PATCHLEVEL );
- XSetStandardProperties( XtDisplay(bw), XtWindow(bw),
- (char *)s, BOARDICON, NULL, NULL, 0, NULL );
-
- }
-
-
-
- static void Redisplay() {
- GbRedisplayBoard (goban);
- }
-
- XtActionsRec actions[] = {
- { "inputStone", (XtActionProc)InputStone },
- { "removeStone", (XtActionProc)RemoveStone },
- { "addStone", (XtActionProc)AlternateStone },
- };
-
- main( argc, argv )
- int argc;
- char **argv;
- {
- extern void exit ();
- extern unsigned char *optarg;
- extern int optind;
- int c;
- int xtargc = 0;
- Arg args[8];
- int j;
- extern int getopt();
-
- get_cmd();
- if( cmd[0] == '\0' ) strcpy( (char *)cmdline, CMD );
- else strcpy( (char *)cmdline, (char *)cmd );
-
- initboard( boardsize );
- move = stepmove = (-1);
- currentmove = (-1);
- statusflag = FALSE;
-
- toplevel = XtAppInitialize( &app_context, "XIgc", NULL, 0, &argc, argv,
- NULL, NULL, 0);
-
- /* any more command-line args? */
- while( (c=getopt( argc, argv, "?c:ds" )) != (-1) ) {
- switch( c ) {
- case 'd': /* debug mode */
- dbg = TRUE;
- break;
- case 's': /* stippled board */
- stippleflag = TRUE;
- break;
- case 'c': /* command line for comm program */
- strcpy( (char *)cmdline, (char *)optarg );
- cmdflag = TRUE;
- break;
- case '?':
- default:
- fprintf( stderr, "%s: Release %s\n", argv[0], PATCHLEVEL );
- fprintf( stderr, USAGE, argv[0] );
- exit( (-1) );
- break;
- }
- }
-
- if( optind != argc ) {
- fprintf( stderr, "%s: Release %s\n", argv[0], PATCHLEVEL );
- fprintf( stderr, USAGE, argv[0] );
- exit( (-1) );
- }
-
- bw = XtCreatePopupShell( "board", topLevelShellWidgetClass, toplevel,
- NULL, 0);
- form = XtCreateManagedWidget( "form", formWidgetClass, toplevel, NULL,
- 0);
- j = 0;
- XtSetArg(args[j], XtNlabel, "Game: " ); ++j;
- XtSetArg(args[j], XtNjustify, XtJustifyLeft ); ++j;
- gamew = XtCreateManagedWidget( "gamew", labelWidgetClass, form,
- args, j );
-
- j = 0;
- XtSetArg(args[j], XtNlabel, "Hcap/Komi: " ); ++j;
- XtSetArg(args[j], XtNjustify, XtJustifyLeft ); ++j;
- hcapw = XtCreateManagedWidget( "hcapw", labelWidgetClass, form,
- args, j );
- j = 0;
- XtSetArg(args[j], XtNlabel, "White: " ); ++j;
- XtSetArg(args[j], XtNjustify, XtJustifyLeft ); ++j;
- whitew = XtCreateManagedWidget( "whitew", labelWidgetClass, form,
- args, j );
- j = 0;
- XtSetArg(args[j], XtNlabel, "Black: " ); ++j;
- XtSetArg(args[j], XtNjustify, XtJustifyLeft ); ++j;
- blackw = XtCreateManagedWidget( "blackw", labelWidgetClass, form,
- args, j );
- j = 0;
- XtSetArg(args[j], XtNlabel, "Captured: " ); ++j;
- XtSetArg(args[j], XtNjustify, XtJustifyLeft ); ++j;
- captw = XtCreateManagedWidget( "captw", labelWidgetClass, form,
- args, j );
- j = 0;
- XtSetArg(args[j], XtNlabel, "Captured: " ); ++j;
- XtSetArg(args[j], XtNjustify, XtJustifyLeft ); ++j;
- captb = XtCreateManagedWidget( "captb", labelWidgetClass, form,
- args, j );
- j = 0;
- XtSetArg(args[j], XtNlabel, "Time: " ); ++j;
- XtSetArg(args[j], XtNjustify, XtJustifyLeft ); ++j;
- timew = XtCreateManagedWidget( "timew", labelWidgetClass, form,
- args, j );
- j = 0;
- XtSetArg(args[j], XtNlabel, "Time: " ); ++j;
- XtSetArg(args[j], XtNjustify, XtJustifyLeft ); ++j;
- timeb = XtCreateManagedWidget( "timeb", labelWidgetClass, form,
- args, j );
- j = 0;
- XtSetArg(args[j], XtNlabel, "Last: " ); ++j;
- XtSetArg(args[j], XtNjustify, XtJustifyLeft ); ++j;
- lastw = XtCreateManagedWidget( "lastw", labelWidgetClass, form,
- args, j );
- j = 0;
- XtSetArg( args[j], XtNgameSize, 19 ); ++j;
- XtSetArg( args[j], XtNcursor, GbCBlackStone); ++j;
- goban = XtCreateManagedWidget( "goban", gobanWidgetClass, bw, args,
- j);
-
- refresh = XtCreateManagedWidget( "refresh", commandWidgetClass, form,
- NULL, 0);
- cursorcolor = XtCreateManagedWidget( "cursorcolor", commandWidgetClass,
- form, NULL, 0);
- clearb = XtCreateManagedWidget( "clearb", commandWidgetClass, form,
- NULL, 0);
- stepf = XtCreateManagedWidget( "stepf", commandWidgetClass, form, NULL,
- 0);
- stepb = XtCreateManagedWidget( "stepb", commandWidgetClass, form, NULL,
- 0);
- refreshstep = XtCreateManagedWidget( "refreshstep", commandWidgetClass,
- form, NULL, 0);
-
- localrefresh = XtCreateManagedWidget( "localrefresh",
- commandWidgetClass, form, NULL, 0);
- size = XtCreateManagedWidget( "size", commandWidgetClass, form, NULL,
- 0);
- savegame = XtCreateManagedWidget( "savegame", commandWidgetClass,
- form, NULL, 0);
- savecurrent = XtCreateManagedWidget( "savecurrent", commandWidgetClass,
- form, NULL, 0);
- coordinates = XtCreateManagedWidget( "coordinates", commandWidgetClass,
- form, NULL, 0);
- quit = XtCreateManagedWidget( "quit", commandWidgetClass, form, NULL,
- 0);
-
- XtAppAddActions (app_context, actions, XtNumber(actions));
-
- XtAddCallback (refresh, XtNcallback, (XtCallbackProc)RefreshHost,
- NULL);
- XtAddCallback (cursorcolor, XtNcallback, (XtCallbackProc)CursorColor,
- NULL);
- XtAddCallback (clearb, XtNcallback, (XtCallbackProc)ClearBoard, NULL);
- XtAddCallback (stepf, XtNcallback, (XtCallbackProc)StepForward, NULL);
- XtAddCallback (stepb, XtNcallback, (XtCallbackProc)StepBack, NULL);
- XtAddCallback (refreshstep, XtNcallback, (XtCallbackProc)RefreshStep,
- NULL);
- XtAddCallback (localrefresh, XtNcallback, (XtCallbackProc)LocalRefresh,
- NULL);
- XtAddCallback (size, XtNcallback, (XtCallbackProc)SetSize, NULL);
- XtAddCallback (savegame, XtNcallback, (XtCallbackProc)SaveGame,
- (XtPointer)REALMOVE );
- XtAddCallback (savecurrent, XtNcallback, (XtCallbackProc)SaveGame,
- (XtPointer)STEPMOVE );
- XtAddCallback (coordinates, XtNcallback, (XtCallbackProc)SetCoordinates,
- NULL);
- XtAddCallback (quit, XtNcallback, (XtCallbackProc)Quit, NULL);
-
- XtRealizeWidget (toplevel);
-
- XSetStandardProperties( XtDisplay(toplevel), XtWindow(toplevel),
- TITLE, ICONTITLE, NULL, argv, argc, NULL );
-
- SetupBoard();
-
- /* grab keystrokes when mouse is in board or menu windows */
- #ifndef NOKEYGRAB
- XtGrabKey( bw, AnyKey, AnyModifier, FALSE,
- GrabModeAsync, GrabModeAsync );
- XtAddEventHandler( bw, KeyPressMask | KeymapStateMask, FALSE,
- (XtEventHandler)keyevent, NULL );
-
- XtGrabKey(toplevel, AnyKey, AnyModifier, FALSE,
- GrabModeAsync, GrabModeAsync);
- XtAddEventHandler(toplevel, KeyPressMask | KeymapStateMask, FALSE,
- (XtEventHandler)keyevent, NULL);
- #endif /* NOKEYGRAB */
-
- setup_filter( &exec_stdin, &exec_stdout, &exec_stderr );
- input0id = XtAppAddInput( app_context, fileno(stdin),
- (XtPointer)(XtInputReadMask), read_kbd,
- (caddr_t)fileno(stdin) );
- input1id = XtAppAddInput( app_context, fileno(exec_stdout),
- (XtPointer)(XtInputReadMask), read_filter,
- (caddr_t)fileno(exec_stdout) );
- input2id = XtAppAddInput( app_context, fileno(exec_stderr),
- (XtPointer)(XtInputReadMask), read_filter,
- (caddr_t)fileno(exec_stderr) );
-
- XtAppAddTimeOut( XtWidgetToApplicationContext(toplevel), 1000, Ticker,
- XtWidgetToApplicationContext(toplevel) );
- XtAppMainLoop (app_context);
- }
-
- Xsetup_p( fr, fw, fe )
- FILE *fr, *fw, *fe;
- {
- i1id = XtAppAddInput( app_context, fileno(fw),
- (XtPointer)(XtInputReadMask), read_pipe,
- (caddr_t)fileno(fw) );
- i2id = XtAppAddInput( app_context, fileno(fe),
- (XtPointer)(XtInputReadMask), read_pipe,
- (caddr_t)fileno(fe) );
- if( dbg ) {
- fprintf( stderr, "Xsetup_p: i1id=0x%x, i2id=0x%x\n", i1id, i2id );
- fflush( stderr );
- }
- }
-
- Xclose_p() {
- XtRemoveInput( i1id );
- XtRemoveInput( i2id );
- }
-
- void read_pipe( client_data, source, id )
- int client_data;
- int *source;
- char *id;
- {
- unsigned char str[LINESIZE+1];
- int ret;
-
- ret = read( client_data, str, LINESIZE );
- if( ret == (-1) ) perror( "read_pipe" );
-
- if( dbg ) {
- fprintf( stderr, "ENTER read_pipe [%d]\n", ret );
- fflush( stderr );
- }
-
- if( ret <= 0 ) return;
- else str[ret] = '\0';
-
- fprintf( stderr, "%s", str );
- fflush( stderr );
- }
-
-
- /* this one reads input from the comm channel, prints it, and also
- * calls process_input for parsing
- */
- void read_filter( client_data, source, id )
- int client_data;
- int *source;
- char *id;
- {
- unsigned char str[LINESIZE+1], *s;
- int ret, i, code;
-
- ret = read( client_data, str, LINESIZE );
- if( ret <= 0 ) {
- /* these lines don't really do anything very useful,
- * and they cause problems on some HP 9000 systems
- XtRemoveInput( input1id );
- XtRemoveInput( input2id );
- fprintf( stderr, "Closing pipes to comm process\n" );
- SC*/
- return;
- }
- else str[ret] = '\0';
-
- if( dbg ) fprintf( stderr,
- "********* in read_filter() fd=%d **********\n", client_data );
-
- /* just check once more to be sure we have only 7-bit chars */
- for( i=0; i<ret; ++i ) str[i] &= 0x7f;
-
- process_input( str );
-
- if( dbg ) fprintf( stderr,
- "######### leave read_filter() [%d chars] #########\n", ret );
- }
-
- /* this one just reads lines from the kbd and passes them to comm stdin */
- void read_kbd( client_data, source, id )
- int client_data;
- int *source;
- char *id;
- {
- unsigned char str[LINESIZE+1];
- int ret;
-
- if( dbg ) fprintf( stderr,
- "********* in read_kbd() fd=%d **********\n", client_data );
- ret = read( client_data, str, LINESIZE );
- if( ret <= 0 ) {
- fprintf( stderr, "\nKBD: no input! (ignored)\n" );
- return;
- }
- else str[ret] = '\0';
-
- /*XXX got to do something about the CTRL-P to make it work right
- * when cursor is in xterm window
- */
-
- process_kbd( str, exec_stdin );
-
- if( dbg ) fprintf( stderr,
- "######### leave read_kbd() [%d chars] #########\n", ret );
- }
-
- setMark( i, j, t )
- int i, j;
- unsigned char t;
- {
- GbSetMark( goban, (Position)i, (Position)j, t, 0 );
- }
-
- drawStone( i, h, t, flag )
- int i, h;
- int t; /* 0=remove, 1=black, 2=white */
- unsigned char flag;
- {
- if( dbg ) fprintf( stderr, "Drawing stone at %d,%d (%d)\n", i, h, t );
- GbSetPoint( goban, i, h, t, flag );
- }
-
- update_status( g, black, white, bcapt, wcapt, btime, wtime, bbyo, wbyo, rankw,
- rankb )
- int g;
- unsigned char *white, *black;
- int wcapt, bcapt, wtime, btime;
- int bbyo, wbyo;
- char *rankw, *rankb;
- {
- Arg args[8];
- int j;
- unsigned char str[80];
-
- update_gameno( g );
-
- if( g < 0 ) sprintf( (char *)str, "Black:" );
- else if( (int)strlen( (char *)rankb ) <= 0 )
- sprintf( (char *)str, "Black: %s", black );
- else sprintf( (char *)str, "Black: %s %s", black, rankb );
- j = 0;
- XtSetArg(args[j], XtNlabel, str ); ++j;
- XtSetValues( blackw, args, j );
-
- if( g < 0 ) sprintf( (char *)str, "White:" );
- else if( (int)strlen( (char *)rankw ) <= 0 )
- sprintf( (char *)str, "White: %s", white );
- else sprintf( (char *)str, "White: %s %s", white, rankw );
- j = 0;
- XtSetArg(args[j], XtNlabel, str ); ++j;
- XtSetValues( whitew, args, j );
-
- if( g < 0 ) sprintf( (char *)str, "Captured:" );
- else sprintf( (char *)str, "Captured: %d", wcapt );
- j = 0;
- XtSetArg(args[j], XtNlabel, str ); ++j;
- XtSetValues( captw, args, j );
-
- if( g < 0 ) sprintf( (char *)str, "Captured:" );
- else sprintf( (char *)str, "Captured: %d", bcapt );
- j = 0;
- XtSetArg(args[j], XtNlabel, str ); ++j;
- XtSetValues( captb, args, j );
-
- fix_wtime( wtime, wbyo );
- fix_btime( btime, bbyo );
- }
-
- update_move( last )
- unsigned char *last;
- {
- int i, j;
- unsigned char str[80];
- Arg args[8];
-
- if( last == NULL ) sprintf( (char *)str, "Last:" );
- else {
- sprintf( (char *)str, "Last: %s", last );
- for( i=0; str[i] != '\0' && str[i] != '\r' && str[i] != '\n'; ++i );
- str[i] = '\0';
- }
-
- j = 0;
- XtSetArg(args[j], XtNlabel, str ); ++j;
- XtSetValues( lastw, args, j );
- }
-
- update_handicap( hcap, komi )
- int hcap;
- float komi;
- {
- Arg args[8];
- int j;
- unsigned char str[80];
-
- if( hcap < 0 ) sprintf( (char *)str, "Hcap/Komi:" );
- else sprintf( (char *)str, "Hcap/Komi: %d / %2.1f", hcap, komi );
- j = 0;
- XtSetArg(args[j], XtNlabel, str ); ++j;
- XtSetValues( hcapw, args, j );
- }
-
- update_gameno( g )
- int g;
- {
- Arg args[8];
- int j;
- unsigned char str[80];
-
- if( g < 0 ) sprintf( (char *)str, "Game:" );
- else sprintf( (char *)str, "Game: %d", g );
- j = 0;
- XtSetArg(args[j], XtNlabel, str ); ++j;
- XtSetValues( gamew, args, j );
- }
-
- /* get the server config file, with command line to execute, if any
- * return (-1) if no
- * 1) a file $HOME/.xigcrc
- * 2) in this file:
- * COMMAND cmd line to execute
- * QUITCOMMAND cmd to quit from "cmd"
- * SAVEFORMAT SGF or TEXT format for "Save game" menu item
- * TERSEMODE true or false
- * SAVEDIR full path of directory to save games into
- * SORTWHO sorts "who" output by player rank
- */
- static int get_cmd() {
- unsigned char *ret;
- FILE *fp;
- unsigned char str[80];
- int cnt = 0;
- extern char *getenv();
- int len;
-
- cmd[0] = '\0';
- quitcmd[0] = '\0';
- strcpy( (char *)savedir, "." );
-
- ret = (unsigned char *)getenv( "HOME" );
- if( ret == NULL ) return( cnt );
-
- sprintf( (char *)str, "%s/.xigcrc", ret );
- if( dbg ) fprintf( stderr, "config file: >>%s<<\n", str );
- fp = fopen( (char *)str, "r" );
- if( fp == NULL ) {
- if( dbg ) fprintf( stderr, "can't open config file >>%s<<\n", str );
- return( cnt );
- }
-
- while( fgets( (char *)str, 80, fp ) != NULL ) {
- len = strlen( (char *)str );
- if( len < 4 || str[0] == '#' ) continue;
- str[len-1] = '\0';
- if( dbg ) fprintf( stderr, ">>%s<<\n", str );
- if( strncmp( (char *)str, "COMMAND", 7 ) == 0 ) {
- if( cmd[0] == '\0' ) {
- strcpy( (char *)cmd, (char *)&str[8] );
- if( dbg ) fprintf( stderr, "get_cmd: cmd >>%s<<\n" , cmd );
- ++cnt;
- }
- }
- if( strncmp( (char *)str, "QUITCOMMAND", 11 ) == 0 ) {
- if( quitcmd[0] == '\0' ) {
- strcpy( (char *)quitcmd, (char *)&str[12] );
- if( dbg ) fprintf( stderr, "get_cmd: quitcmd >>%s<<\n",
- quitcmd );
- ++cnt;
- }
- }
- if( strncmp( (char *)str, "TERSEMODE", 9 ) == 0 ) {
- if( strncmp( (char *)&str[10], "TRUE", 4 ) == 0 ||
- strncmp( (char *)&str[10], "true", 4 ) == 0 )
- tersemode = TRUE;
- else if( strncmp( (char *)&str[10], "FALSE", 5 ) == 0 ||
- strncmp( (char *)&str[10], "false", 5 ) == 0 )
- tersemode = FALSE;
- ++cnt;
- }
- if( strncmp( (char *)str, "SORTWHO", 7 ) == 0 ) {
- if( strncmp( (char *)&str[8], "TRUE", 4 ) == 0 ||
- strncmp( (char *)&str[8], "true", 4 ) == 0 )
- sortwhomode = TRUE;
- else if( strncmp( (char *)&str[8], "FALSE", 5 ) == 0 ||
- strncmp( (char *)&str[8], "false", 5 ) == 0 )
- sortwhomode = FALSE;
- ++cnt;
- }
- if( strncmp( (char *)str, "SAVEFORMAT", 10 ) == 0 ) {
- if( strncmp( (char *)&str[11], "TEXT", 4 ) == 0 ||
- strncmp( (char *)&str[11], "text", 4 ) == 0 )
- saveformat = TEXT;
- else if( strncmp( (char *)&str[11], "SGF", 3 ) == 0 ||
- strncmp( (char *)&str[11], "sgf", 3 ) == 0 )
- saveformat = SGF;
- ++cnt;
- }
- if( strncmp( (char *)str, "SAVEDIR", 7 ) == 0 ) {
- strcpy( (char *)savedir, (char *)&str[8] );
- ++cnt;
- }
- }
-
- fclose( fp );
- return( cnt );
- }
-
- fix_wtime( wtime, wbyo )
- int wtime;
- int wbyo;
- {
- Arg args[8];
- int j;
- unsigned char str[80];
-
- if( wbyo > 0 ) {
- sprintf( (char *)str, "Byo Yomi: %d:%02.2d %d",
- wtime/60, wtime%60, wbyo );
- }
- else {
- sprintf( (char *)str, "Time: %d:%02.2d", wtime/60, wtime%60 );
- }
- j = 0;
- XtSetArg(args[j], XtNlabel, str ); ++j;
- XtSetValues( timew, args, j );
- }
-
- fix_btime( btime, bbyo )
- int btime, bbyo;
- {
- Arg args[8];
- int j;
- unsigned char str[80];
-
- if( bbyo > 0 ) {
- sprintf( (char *)str, "Byo Yomi: %d:%02.2d %d",
- btime/60, btime%60, bbyo );
- }
- else {
- sprintf( (char *)str, "Time: %d:%02.2d", btime/60, btime%60 );
- }
- j = 0;
- XtSetArg(args[j], XtNlabel, str ); ++j;
- XtSetValues( timeb, args, j );
- }
-